package leetcode.datastructure.array;

public class Program238ProductExceptSelf {

    public Program238ProductExceptSelf() {

    }

    public int[] productExceptSelf(int[] nums) {
        int res = 1;
        int count = 0;
        for (int num : nums) {
            if (num != 0) {
                res *= num;
            } else {
                count++;
            }
        }
        int[] tem = new int[nums.length];
        if (count > 1) {
            return tem;
        } else if (count == 1) {
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] == 0) {
                    tem[i] = res;
                    return tem;
                }
            }
        } else {
            for (int i = 0; i < nums.length; i++) {
                tem[i] = res / nums[i];
            }
        }
        return tem;
    }

    public static void main(String[] args) {
        Program238ProductExceptSelf obj = new Program238ProductExceptSelf();
        int[] nums = {1,2,3,4};
        int[] ints = obj.productExceptSelf(nums);
        System.out.println(ints[0]);
        int[] nums1 = {-1,1,0,-3,3};
        int[] ints1 = obj.productExceptSelf(nums1);
        System.out.println(ints1[0]);
    }

}
